package com.itheima.d10_stream_end_method;

import com.itheima.d09_stream_middle_method.Student;

import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

/**
 * 目标：掌握Stream常用的终结方法
 */
public class Test {

    public static void main(String[] args) {
        //准备测试数据：
        List<Student> students = new ArrayList<>();
        students.add(new Student("小王", 18, 66));


        students.add(new Student("小张", 17, 99));
        students.add(new Student("小李李", 26, 20));
        students.add(new Student("小赵", 16, 77));
        students.add(new Student("小刘刘", 19, 18));
        students.add(new Student("小李", 26, 20));


        //需求1：找出所有三个字的学生，统计输出数量
        long count = students.stream()
                .filter(s -> s.getName().length() == 3)
                .count();
        System.out.println("三个字学生数量："+count );


        //需求2：找出所有姓名两个字的学生中年纪最大的学生年纪
        //Optional封装了统计结果
        Optional<Student> opt = students.stream()
                .filter(s -> s.getName().length() == 2)
                .max((s1, s2) -> s1.getAge() - s2.getAge());
        Student student = opt.get();
        System.out.println("年龄最大的学生："+student);


        //需求3：找出考试最后一名学生信息
        Optional<Student> opt2 = students.stream()
                .min((s1, s2) -> Double.compare(s1.getScore(), s2.getScore()));
        Student student2 = opt2.get();
        System.out.println("考试最后学生："+student2);


        System.out.println("======================");

        //需求4：找出所有成绩合格的学生，返回学生数组
        Student[] array = students.stream()
                .filter(s -> s.getScore() >= 60)
                .toArray(len -> new Student[len]); //指定创建数据的类型和长度

        for (Student student1 : array) {
            System.out.println(student1);
        }

        System.out.println("======================");

        //需求5：找出所有成绩不合格的学生，返回学生集合
        List<Student> collect = students.stream()
                .filter(s -> s.getScore() < 60)
                .collect(Collectors.toList());

        System.out.println( collect);

        Set<Student> collect1 = students.stream()
                .filter(s -> s.getScore() < 60)
                .collect(Collectors.toSet());

        System.out.println( collect1);

        Map<String, Double> collect2 = students.stream()
                .filter(s -> s.getScore() < 60)
                .collect(Collectors.toMap( s-> s.getName() ,  s-> s.getScore() ));

       collect2.forEach((k,v)-> System.out.println(k+"---->"+v));


    }
}














